package server;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import net.bican.wordpress.Page;
import net.bican.wordpress.Wordpress;
import redstone.xmlrpc.XmlRpcException;
import redstone.xmlrpc.XmlRpcFault;
import redstone.xmlrpc.XmlRpcMessages;


public class TestWp {

	/**
	 * @param args
	 */
	public static String postWp(String username,String password,String domain) throws XmlRpcFault, IOException
	{

		    String xmlRpcUrl = domain+"/xmlrpc.php";
		    String title = "Testing post from hatforrent";
		    String description = "Testing post from <a href='http://hatforrent.com'>hatforent</a>";
		    String result="";
		    try {
		    	Wordpress wp = new Wordpress(username, password, xmlRpcUrl);
		    	
		    	Page recentPost = new Page();
			    recentPost.setDateCreated(new Date());
			    recentPost.setPost_status("publish");
			    recentPost.setWp_author("admin");
			    recentPost.setTitle(title);

			    recentPost.setDescription(description);

			   result = wp.newPost(recentPost, false);
			} catch (XmlRpcException e) {
				e.printStackTrace();
			}
			return result;

	}
	public static void main(String[] args) throws IOException {
		
//		URL url = new URL("http://www.videogamestechnology.com/xmlrpc.php");
//		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//		 connection.setDoInput( true );
//	       
//	        connection.setDoInput( true );
//	        connection.setDoOutput( true );
//	         connection.setRequestMethod( "POST" );
//	         //connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8" );
//	         
//	         BufferedWriter writer = new BufferedWriter(
//	                    new OutputStreamWriter(
//	                        connection.getOutputStream(),
//	                        "utf-8" ) );
//	         writer.write( "<?xml version=\"1.0\" encoding=\"" );
//	            writer.write("utf-8" );
//	            writer.write( "\"?>" );
//	            writer.write( "<methodCall><methodName>" );
//	            writer.write( "wp.newPost" );
//	            writer.write( "</methodName><params>" );
//	            writer.write( "<param><value>0</value></param>" );
//	            writer.write( "<param><value>admin</value></param>" );
//	            writer.write( "<param><value>longlife4722</value></param>" );
//	            writer.write( "<param><value><title>abc</title><description>bbb</description><post_type>post</post_type></value></param>" );
//	            writer.write( "<param><value>true</value></param>" );
//	            
//	            writer.write( "</params>" );
//	            writer.write( "</methodCall>" );
//	            writer.flush();
//	            BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream()) );
//	            while(1==1)
//	            {
//	            	String str= reader.readLine();
//	            	if(str==null) break;
//	            	System.out.println(str);
//	            	
//	            }
	        
		try {
			postWp("admin","longlife4722","http://www.videogamestechnology.com");
		} catch (XmlRpcFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
